//
// namespace.cs: Tracks namespaces
//
// Author:
//   Miguel de Icaza (miguel@ximian.com)
//   Marek Safar (marek.safar@seznam.cz)
//
// Copyright 2001 Ximian, Inc.
// Copyright 2003-2008 Novell, Inc.
// Copyright 2011 Xamarin Inc
//
using System;
using System.Collections.Generic;
using System.Linq;
using Mono.CompilerServices.SymbolWriter;

namespace Teal.Compiler {
    //
	// Namespace cache for imported and compiled namespaces
	//

    public partial class SourceUnit : MemberContainerDefinition
	{
		readonly SourceFile2 file;
		CompileUnitEntry comp_unit;
		Dictionary<string, SourceFile2> include_files;


        public SourceUnit() { }

		public SourceUnit (ModuleDefinition parent, SourceFile2 sourceFile) 
		{
			this.file = sourceFile;
		}

		public CompileUnitEntry SymbolUnitEntry {
			get {
				return comp_unit;
			}
		}

		public override void PrepareEmit ()
		{
			var sw = containerModule.DeclaringAssembly.SymbolWriter;
			if (sw != null) {
				CreateUnitSymbolInfo (sw);
			}

			base.PrepareEmit ();
		}

		//
		// Creates symbol file index in debug symbol file
		//
		void CreateUnitSymbolInfo (MonoSymbolFile symwriter)
		{
			var si = file.CreateSymbolInfo (symwriter);
			comp_unit = new CompileUnitEntry (symwriter, si);

			if (include_files != null) {
				foreach (SourceFile2 include in include_files.Values) {
					si = include.CreateSymbolInfo (symwriter);
					comp_unit.AddFile (si);
				}
			}
		}
	}

}
